/*
 * Main - start here (for the client)
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.Lib;
import flash.display.Stage;
import flash3f.Images;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash3f.InputHandler;
import flash3f.FlashConsole;

import mochiads.Mochiad;

import PopTart;

#else neko
import neko3f.InputHandler;
import neko.Sys;
import neko.io.File;
import neko.FileSystem;

#end

import lib3f.LCG;

import ComponentManager;
import GameTile;
import Graph;

class Main
{		

	#if flash9
	public static var timer : Timer = new Timer(30,0);
	public static var bg : TileBG;
	public static var location : String = "spacecreeps.com";
	public static var adtime : Int = 0;
	public static var console : FlashConsole;
	public static var playrecording : Int = -1;
	#else neko
	public static var cont : Bool;
	public static var gameobj : CreepData;
	#end

	//public static var g : Graph2D;
	static public var version : Int = 2;
	static public var name : String = "Guest";
	static public var group : String = "spacecreeps.com";
	
	public function new() 
	{		
	
	
		#if flash9
			//mochiads.Mochiad.showPreloaderAd({id:"3e83514e0b888d97", res:"680x425", clip:Lib.current, ad_finished:starttitle});
			//timer.addEventListener(TimerEvent.TIMER, waitonad);		
 			timer.start();
			//console = new FlashConsole({placetowers:PlaceTowers,title:Title});

			SFX.soundon = true;
			try
			{
				Title.settings = flash.net.SharedObject.getLocal("settings");
				Main.name = Title.settings.data.name;
				Main.group = Title.settings.data.group;			
				SFX.soundon = Title.settings.data.sound;		
			}
			catch(foo : Dynamic)
			{
				SFX.soundon = true;
			}
			
			amirecording();
			
			InputHandler.start();
			SFX.load();
			ComponentManager.add(SFX);
			Images.start(new Gfx());
			//ComponentManager.timer.addEventListener(TimerEvent.TIMER, bah); /// for loading effects
			starttitle();
		#else neko
			CreepData.loadDbInfo();
			var args = Sys.args();
			trace(args);
			Recorder.dbconnect();
			gameobj = CreepData.manager.get(Std.parseInt(args[0]));
			Recorder.unserialize(gameobj.recordingdata);
			trace(Recorder.recordinglength);
			trace("loaded, begin");
			loop();
			// process gets terminated from Recorder
		#end
	}
			
	public static function loop()
	{
	
		#if flash9
							
			LCG.timeseed();
			Title.seed = LCG.seed;
			
		#else neko

		StartGame.playrecording = true;
		
		cont = true;
		
		ComponentManager.add(StartGame);
		
			while(cont)
			{
				ComponentManager.up();
			}	
		
		#end
		
	}

	#if flash9
	public static function waitonad(e : TimerEvent)
	{
		if (adtime>360) // if the ad code isn't showing after 10 seconds...
		{
			timer.removeEventListener(TimerEvent.TIMER, waitonad);		
			timer.addEventListener(TimerEvent.TIMER, letadfade);
			adtime = 0;
		}
		else
		{
			adtime+=1;
		}
	}
	
	public static function letadfade(e : TimerEvent)
	{
		if (adtime>15) // Allow 0.5 secs of fade
		{
			timer.removeEventListener(TimerEvent.TIMER, letadfade);		
			starttitle();
		}
		else
		{
			adtime+=1;
		}
	}

	public static function amirecording()
	{
		var params : Dynamic = Lib.current.root.loaderInfo.parameters;
		try
		{
			playrecording = params.rec;
		}
		catch(d : Dynamic) {}
	
		if (playrecording == 0)
			playrecording = -1; // there is no recording 0 and it's the default empty value
	
	}
	#end

	#if flash9
	public static function starttitle()
	{
		ComponentManager.add(Control);
		if (playrecording==-1)
		{
			timer.addEventListener(TimerEvent.TIMER, ComponentManager.up);			
			ComponentManager.add(GameFace);
			Networker.go("Numdownload");
		}
		else
		{	
			Title.stars = new Starfield();
			Lib.current.addChild(Title.stars);
			for (n in 0...1000)
			{
				Title.stars.up();
			}
			Networker.go("Recdownload");
		}
	}
	#end
	
	public static function main()
	{

		new Main ();		
	}
}
